/* 显示欢迎信息 */
function showHello() {
	var str;
	var d = new Date();	
	h = d.getHours();
	if(h<6) str="凌晨好";
	else if (h<9) str="早上好";
	else if (h<12) str="上午好";
	else if (h<14) str="中午好";
	else if (h<17) str="下午好";
	else if (h<19) str="傍晚好";
	else if (h<22) str="晚上好";
	else str="夜里好";
	$("#sayhello").text( str );
}
showHello();

/**
 * 多表单、多按钮提交到不同的地方
 * @param string sformname :指定要对哪个表单操作
 * @param string saction : 指定到表单的动作
 * @return nothing;
 */
function m_submit( form_id, form_action ) {
	document.getElementById( form_id ).action=form_action;
	$('#'+form_id).submit();
}

/*
显示隐藏的tip Menu
*/
function showTipMenu( obj, uid ) {
	var x,y;
	$(obj).mouseover(function(e){
		//首先隐藏其它的tip menu
		//alert ( $("#tipMenu").html() );
		if ( $("#tipMenu").val() != null ) {
			hideTipMenu();
		}		
		//alert(e.pageX);
		var x = e.pageX + 15;
		var y = e.pageY + 10;
		//alert(x);alert(y);
		$("body").append( createTipMenu( uid ) );
		//alert( createTipMenu( uid ));
		//然后再打开所需要显示的块
		$("#tipMenu").css({ left:x, top:y, display:'block' });
	});
}
//关闭tip menu
function hideTipMenu() { $("#tipMenu").remove(); }
/*
产生所需要的TipMenu层
所用到的样式 #tipMenu 见alumni.css文件
html:  
<div id="tipMenu">
	<a href="index.php?m=member&a=view&id={$creator}" class="r" >查看会员信息</a><br />
	<a href="index.php?m=sms&a=send&to={$creator}" class="r" >传递小纸条</a><br />　　　
	<a onclick="hideTipMenu(this);" class="bk">关闭</a>
</div>*/
function createTipMenu( uid ) {
	var str;
	str = "<div id=\"tipMenu\"><a href=\"" + encodeURI("index.php?m=member&a=view&id=" + uid) + "\" class=\"r\" >查看会员信息</a><br /><a href=\"" + encodeURI("index.php?m=sms&a=send&to=" + uid) + "\" class=\"r\" >传递小纸条</a><br />　　　<a onclick=\"hideTipMenu(this);\" class=\"bk\">关闭</a></div>";
	return str;
}

//jquery的$(document).ready();在FF中不能在页面刚打开时调用focus(),所以直接用window.onload
//window.onload = function() { $("#uid").focus(); };
//检查用户名是否已注册 ajax
function check_user() {
	var uid = $("#uid").val();
	if ( uid != "" ) {
		$.get( encodeURI("index.php?m=login&a=check&uid_tmp=" + uid), 
				function( txt ){
					//alert(txt); 
					if ( txt == 1 ) {
						$("#altmsg").removeClass("b_red").addClass("s_green").text("用户名输入正确");
					} else if ( txt == 0 ) {
						$("#altmsg").removeClass("s_green").addClass("b_red").text("抱歉，此用户不存在，请重新输入");
						$("#uid").focus().select();
						return false;
					} else {
						return false;
					}
				} );
	}
}

//用户ajax登录
function login_user() {
	var uid = $("#uid").val();
	var pass = $("#pass").val();
	//alert(uid);alert(pass);
	//check_user();
	if ( uid == "" || pass == "" ) { 
		//alert( 'false xxx' ); 
		return false; 
	}
	
	$.ajax({
			type: "POST",
			url: "index.php?m=login&a=in",
			dataType:"html",
			data: {id: encodeURI(uid), pass: pass},
			success: function(txt) {
				if ( txt == 1 ) {
					$("#altmsg").removeClass("b_red").addClass("s_green").text("登录成功，正在转入主页面!");
					document.location.href = "./";
				} else if ( txt == 0 ) {
					$("#altmsg").removeClass("s_green").addClass("b_red").text("用户不存在或未审核通过.");
					$("#uid").focus().select();
				} else if ( txt == -1 || txt == -2 ) {
					$("#altmsg").removeClass("s_green").addClass("b_red").text("用户名或密码位数错误，请重试!");
					$("#uid").focus().select();
				} else if ( txt == -3 ) {
					$("#altmsg").removeClass("s_green").addClass("b_red").text("用户ID错误，请重试!");
					$("#uid").focus().select();
				} else if ( txt == -4 ) {
					$("#altmsg").removeClass("s_green").addClass("b_red").text("用户名或密码错误，请重试!");
					$("#uid").focus().select();
				} else if ( txt == -5 ) {
					$("#altmsg").removeClass("s_green").addClass("b_red").text("此用户暂未审核，请等待或者联系管理员!");
					$("#uid").focus().select();
				}
			}
	});
}
//接收键盘的回车键,并注意ajax不要使用submit事件
function chk ( enter ) {
	if(enter == 13 ){login_user();}
}

var uid_ok = false;
var pass_ok = false;
var name_ok = false;
var sex_ok  = false;
var qq_ok = false;
var mobile_ok = false;
var birthday_ok = false;

//检查用户名是否已注册 ajax
function check_userreg() {
	var uid = $("#uid input").val();
	//alert( uid );
	if ( uid != "" ) {
		$.post( "./index.php?m=login&a=check", {uid_tmp: encodeURI(uid)}, 
				function( txt ){
					if ( txt == 0 ) {
						$("#uid span").removeClass("b_red").addClass("s_green").text("恭喜，此用户名可以注册");
						uid_ok = true;
					} else if ( txt == 1 ) {
						$("#uid span").removeClass("s_green").addClass("b_red").text("抱歉，此用户已存在，请重新选择");
						$("#uid input").focus().select();
					} else {
						return false;
					}
				} );
	}
}

//检查密码是否规则且两次相等
//参数num:single 确认一个密码是否OK，double:确认两次密码是否相等
function check_pass( num ) {
	var pass = $("#pass input").attr('value');
	var rpass = $("#rpass input").attr('value');

	if ( pass == "" ) { return false; }
	if ( num == "single" ) {
		if ( pass.length < 4 ) {
			$("#pass span").removeClass("s_green").addClass("b_red").text("密码长度至少4位");
		} else {
			$("#pass span").removeClass("b_red").addClass("s_green").text("正确");
		}
	} else if ( num == "double" ) {
		if ( pass == rpass ) {
			if ( rpass == "" ) { return false; }
			$("#rpass span").removeClass("b_red").addClass("s_green").text("正确");
			pass_ok = true; //密码确认OK
		} else {
			$("#rpass span").removeClass("s_green").addClass("b_red").text("两次密码不相等");
		}
	} else { return false; }
}

//检查姓名
function check_name() {
	var name = $("#name input").attr("value");
	//alert (name.length);
	if ( name != "" ) {
		$("#name span").removeClass("b_red").addClass("s_green").text("正确");
		name_ok = true;
	} else {
		return false;
	}
}

//检查性别
function check_sex(obj) {
	var sex = obj.value;
	var sex_n = "";
	sex_n = ( sex == 0 ? "帅哥你好！" : "美女你好！" );
	$("#sex span").removeClass("b_red").addClass("s_green").text(sex_n);
	sex_ok = true;
}

//电话
function check_mobile() {
	var mobile = $("#mobile input").val();
	//alert(mobile);
	if ( mobile != "" ) {
		//alert(mobile);
		$("#mobile span").removeClass("b_red").addClass("s_green").text("正确");
		mobile_ok = true;
	} else {
		return false;
	}
}

//QQ
function check_qq() {
	var qq = $("#qq input").val();
	//alert(qq);
	if ( qq != "" ) {
		if ( isNumber( qq ) ) {
			$("#qq span").removeClass("b_red").addClass("s_green").text("正确");
			qq_ok = true;
			//alert(qq_ok);
		} else {
			$("#qq span").removeClass("s_green").addClass("b_red").text("请输入正确的QQ号");
		}
	} else {
		return false;
	}
}

//检查生日
function check_birthday() {
	var birthday = $("#birthday input").val();
	//alert(birthday);
	if ( birthday != "" ) {
		$("#birthday span").removeClass("b_red").addClass("s_green").text("正确");
		birthday_ok = true;
	} else {
		return false;
	}
}

//数值检测
function isNumber( name ) {
	if(name.length == 0) return false;
	for(i = 0; i < name.length; i++) {
		if(name.charAt(i) < "0" || name.charAt(i) > "9")
		return false;
	}
	return true;
}

//检查表单并确定是否可以提交
function check_all () {
	if ( uid_ok && pass_ok && name_ok && sex_ok && mobile_ok && qq_ok && birthday_ok ) { return true;}
	else { return false; }
}

function chksubmit() { //按回车按钮
	if( $("#idedit").parent().css('display') == 'block' ) {
		$("#idedit").click();
	} else {
		$("#idadd").click();
	}
}
